/* Author      : Mark Kahn (cwolves@gmail.com)
 * Description : "Fixes" JavaScript/PHP/C++/etc code indentation by throwing away most starting indentation and replacing it based on braces & parenthesis. The script will ignore spacing that is inside sets of parenthesis, i.e. function variables that are aligned.
 * Known Bugs  : Does not ignore PHP "heredoc" strings: e.g. "<<<EOF .... EOF;" blocks. It will format these as it would any other block of code.
 * 
 * Notes:
 * * You must download the javascript file from the google repository to your local machine
 * 
 * Command : cscript //NoLogo "c:\path to file\file.js"
 * Check   : "Run as text filter"
 * 
 * Example:
 * 
 * Input:
 * ------
 * function foo(a,
 *              b,
 *              c){
 * while(bar){
 *        if(x){
 * for(i=0; i<10; i++){
 * //do stuff;
 * }
 * }
 *     }
 * }
 * ------
 * 
 * Output:
 * ------
 * function foo(a,
 *              b,
 *              c){
 * 	while(bar){
 * 		if(x){
 * 			for(i=0; i<10; i++){
 * 				//do stuff;
 * 			}
 * 		}
 * 	}
 * }
 * ------
 */
var stdin        = WScript.StdIn;
var stdout       = WScript.StdOut;
var input        = stdin.ReadAll();

var lines        = input.split('\n');
var output       = [];

String.prototype.trim  = function() {return this.replace(/^[ \t]+/,'').replace(/[ \t]+$/,'');}

var countParen   = 0;
var countIndent  = 0;
var startSpaces  = [];

for(var i=0, m=lines.length; i<m; i++){
	var lineTrimmed  = lines[i].trim();
	if(lineTrimmed==''){ output.push(''); continue; }

	var line         = lineTrimmed.replace(/(\'[^\']*\'|\"[^\"]*\")/g, '');
	while(/\([^\)\(]*\)/g.test(line)){ line = line.replace(/\([^\)\(]*\)/g,  ''); }
	while(/\{[^\}\{]*\}/g.test(line)){ line = line.replace(/\{[^\}\{]*\}/g, '');  }
	while(/\/\//g.test(line)){         line = line.replace(/\/\/.*/g, '');        }

	var openParen   = line.split('(').length - 1;
	var closeParen  = line.split(')').length - 1;
	var openIndent  = line.split('{').length - 1;
	var closeIndent = line.split('}').length - 1;

	if(lineTrimmed.indexOf('}')==0) { // starts with }
		var startingClose = 0;
		while(lineTrimmed.substr(startingClose, 1)=='}'){ startingClose++; }
		var start = new Array(countIndent+1-startingClose).join('\t');
	}else{
		addSpaces = 0;
		if(countParen>0){
			var addSpaces    = countOpeningSpaces(lines[i]) - startSpaces[0];
			if(addSpaces<0){ addSpaces = 0; };
		}
		var start        = (new Array(countIndent+1).join('\t'))+(addSpaces?(new Array(addSpaces+1).join(' ')):'');
	}

	output.push(start+lineTrimmed);

	for(var j=0; j<closeParen; j++){ startSpaces.pop(); }
	for(var j=0; j<openParen; j++){
		startSpaces.push(j<openParen-1?0:countOpeningSpaces(lines[i]));
	}

	countParen  += openParen - closeParen;
	countIndent += line.split('{').length - line.split('}').length;
}

function countOpeningSpaces(str){
	str = str.replace(/\t/g, '   ');
	return str.length - str.replace(/^ +/, '').length;
}